﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerBagManager : Singleton<PlayerBagManager> 
{
    [SerializeField] private GameObject foodIcons;
    [SerializeField] private GameObject medicalIcons;

    public Text text_bananaCount;
    public Text text_breadCount;
    public Text text_carrotCount;
    public Text text_fishCount;

    public Text text_primaryMedicalCount;
    public Text text_middleMedicalCount;
    public Text text_seniorMedicalCount;
    public Text text_superMedicalCount;

    private Dictionary<string, int> itemDict = new Dictionary<string, int>();
    private Dictionary<string, int> foodDict = new Dictionary<string, int>();
    private Dictionary<string, int> medicalDict = new Dictionary<string, int>();

    private void Start()
    {
        itemDict.Add("Banana", 1);
        itemDict.Add("Bread", 0);
        itemDict.Add("Carrot", 0);
        itemDict.Add("Fish", 0);
        itemDict.Add("PrimaryMedical", 1);
        itemDict.Add("MiddleMedical", 0);
        itemDict.Add("SeniorMedical", 0);
        itemDict.Add("SuperMedical", 0);

        //食物补充体力值
        foodDict.Add("Banana", 10);
        foodDict.Add("Bread", 30);
        foodDict.Add("Carrot", 50);
        foodDict.Add("Fish", 70);

        //药品补充血量
        medicalDict.Add("PrimaryMedical", 15);
        medicalDict.Add("MiddleMedical", 35);
        medicalDict.Add("SeniorMedical", 60);
        medicalDict.Add("SuperMedical", 80);

        UpdateUIBag();
    }

    public void OnClickFoodTop()
    {
        foodIcons.SetActive(true);
        medicalIcons.SetActive(false);
    }

    public void OnClickMedicalTop()
    {
        foodIcons.SetActive(false);
        medicalIcons.SetActive(true);
    }

    public void DecreaseItem(string itemName)
    {
        if(itemDict[itemName] == 0)
        {
            return;
        }
        
        int value = (itemDict[itemName] - 1);
        itemDict[itemName] = value;

        IncreasePlayerHealthOrStrength(itemName);
        UpdateUIBag();
    }

    public void IncreaseItem(string itemName)
    {
        int value = (itemDict[itemName] + 1);
        itemDict[itemName] = value;

        UpdateUIBag();
    }

    private void UpdateUIBag()
    {
        text_bananaCount.text = "x" + itemDict["Banana"].ToString();
        text_breadCount.text = "x" + itemDict["Bread"].ToString();
        text_carrotCount.text = "x" + itemDict["Carrot"].ToString();
        text_fishCount.text = "x" + itemDict["Fish"].ToString();
        text_primaryMedicalCount.text = "x" + itemDict["PrimaryMedical"].ToString();
        text_middleMedicalCount.text = "x" + itemDict["MiddleMedical"].ToString();
        text_seniorMedicalCount.text = "x" + itemDict["SeniorMedical"].ToString();
        text_superMedicalCount.text = "x" + itemDict["SuperMedical"].ToString();
    }

    private void IncreasePlayerHealthOrStrength(string itemName)
    {
        if(foodDict.ContainsKey(itemName))
        {
            int value = foodDict[itemName];
            float strength = GameManager.GetInstance().GetStrength;
            strength = strength + value;
            GameManager.GetInstance().SetStrength = strength >= 100f ? 100f : strength;
            return;
        }
        if (medicalDict.ContainsKey(itemName))
        {
            int value = medicalDict[itemName];
            float hp = GameManager.GetInstance().GetHp;
            hp = hp + value;
            GameManager.GetInstance().SetHp = hp >= 100f ? 100f : hp;
        }
    }

    

}
